package com.sljy.hzk.view.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.sljy.hzk.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created  on 2018/7/3 0003
 *
 * @author 唐开阔
 * @describe 选择图片适配器
 */
public class ChosePicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<String> picPaths;
    private final static int TYPE_PIC = 0;//图片
    private final static int TYPE_ADD = 1;//add
    public OnChosePicListener listener;
    private int maxSize;
    ImageButton tvAdd;

    public void setListener(OnChosePicListener listener) {
        this.listener = listener;
    }

    public ChosePicAdapter(Context context, @NonNull List<String> picPaths,int maxSize) {
        this.context = context;
        this.picPaths = picPaths;
        this.maxSize = maxSize;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE_PIC:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chosepic_pic, parent, false);
                return new PicViewHolder(view);
            case TYPE_ADD:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chosepic_add, parent, false);
                return new AddViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case TYPE_PIC:
                PicViewHolder picViewHolder = (PicViewHolder) holder;
                Glide.with(context).load(picPaths.get(position)).into(picViewHolder.imgPic);
                break;
            case TYPE_ADD:
                AddViewHolder addViewHolder = (AddViewHolder) holder;
                //tvAdd.setText("\n\n\n\n\n" + picPaths.size() + "/"+maxSize);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return picPaths.size() == maxSize ? picPaths.size() : picPaths.size()+1;
    }

    @Override
    public int getItemViewType(int position) {
        if (picPaths.size() == maxSize){
            return TYPE_PIC;
        }else {
            return position == picPaths.size() ? TYPE_ADD : TYPE_PIC;
        }
    }

    public class PicViewHolder extends RecyclerView.ViewHolder {
        ImageView imgPic;
        ImageView imgDelete;

        public PicViewHolder(View itemView) {
            super(itemView);
            imgPic = itemView.findViewById(R.id.imageView);
            imgDelete = itemView.findViewById(R.id.img_delete);
            imgDelete.setOnClickListener(v -> {
                picPaths.remove(getAdapterPosition());
                notifyItemRemoved(getAdapterPosition());
                //tvAdd.setText("\n\n\n\n\n" + picPaths.size() + "/"+maxSize);
            });
        }
    }

    public class AddViewHolder extends RecyclerView.ViewHolder {

        public AddViewHolder(View itemView) {
            super(itemView);
            tvAdd = itemView.findViewById(R.id.tv_add);
            tvAdd.setOnClickListener(v -> {
                if (picPaths.size() == maxSize){
                    Toast.makeText(context, "最多选择"+maxSize+"张图片", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (listener != null) {
                    listener.onAddClick();
                }
            });
        }
    }


    public void OnTvAddClick(){
        if (picPaths.size() == maxSize){
            Toast.makeText(context, "最多选择"+maxSize+"张图片", Toast.LENGTH_SHORT).show();
            return;
        }
        if (listener != null) {
            listener.onAddClick();
        }
    }


    public void hideTvAdd(){
        if(tvAdd!= null)
            tvAdd.setVisibility(View.GONE);
    }

    public interface OnChosePicListener {
        void onAddClick();
    }
}
